package cn.bdqn.dao.impl;


import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;

import cn.bdqn.dao.UsersDao;
import cn.bdqn.pojo.Users;

@Repository("userDao")
public class UsersDaoImpl extends HibernateDaoSupport implements UsersDao {
	
	//构造注入
	public UsersDaoImpl() {}
	@Autowired	
	public UsersDaoImpl(@Qualifier("sessionFactory")SessionFactory  sessionFactory) {
	 this.setSessionFactory(sessionFactory);
	}

	public Users getUserByUcode(String userCode) {
		/*// TODO Auto-generated method stub
		String queryHql ="from Users where userCode =?";
		//List<Users> list = this.getHibernateTemplate().find(queryHql, userCode);
		Users u =(Users) this.getHibernateTemplate().find(queryHql, userCode).get(0);*/
		Users u  =null;
		Session  session = this.getHibernateTemplate().getSessionFactory().openSession();
		try {
			String queryHql ="from Users where userCode =:ucode";
			u  = (Users) session.createQuery(queryHql).setParameter("ucode", userCode).list().get(0);
					
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}finally{
			if(session!=null) session.close();
		}
		
		
		return u;
	}

}
